<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/21
 * Time: 9:36
 */

namespace app\admin\controller;


use app\admin\model\GoodsCategory;
use app\admin\model\GoodsList;

class Goods extends Base
{

    public function categoryList(){
        return $this->fetch();
    }
    public function categoryData($page,$limit){
        $where = input('get.where')?[['category_name','like','%'.trim(input('get.where/s')).'%']]:'';
        $data = GoodsCategory::where($where)->order('pid asc')->page($page,$limit)->select()->each(function($item){
            if ($item->pid){
                $item->pid = GoodsCategory::where('id',$item->pid)->value('category_name');
            }else{
                $item->pid = '一级分类';
            }
        })->toArray();
        $count = GoodsCategory::where($where)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function categoryAdd(){
        if (request()->isPost()){
            $post = input('post.');
            $ret = GoodsCategory::create($post);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $data = GoodsCategory::where('pid',0)->field('id,category_name')->select()->toArray();
        $this->assign('one',$data);
        return $this->fetch();
    }
    public function categoryEdit($id){
        if (request()->isPost()){
            $post = input('post.');
            $where = ['id'=>$post['id']];
            unset($post['id']);
            $ret = GoodsCategory::update($post,$where);
            if (!$ret){
                return json(['code'=>0,'msg'=>'保存失败']);
            }
            return json(['code'=>1,'msg'=>'保存成功']);
        }
        $info = GoodsCategory::where('id',$id)->find();
        $data = GoodsCategory::where('pid',0)->field('id,category_name')->select()->toArray();
        $this->assign('one',$data);
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function categoryDel(){
        $id = input('post.id');
        $ret = GoodsCategory::destroy($id);
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }

    public function goodsList(){
        return $this->fetch();
    }
    public function goodsData($page,$limit){
        $where = input('get.where')?[['good_name|price','like','%'.trim(input('get.where')).'%']]:'';
        $data = GoodsList::where($where)->page($page,$limit)->select()->toArray();
        $count = GoodsList::where($where)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function goodsAdd(){
        if (request()->isPost()){

        }
        $data = GoodsCategory::where('status',1)->where('pid',0)
            ->field('id,category_name')->select()->toArray();
        $this->assign('data',$data);
        return $this->fetch();
    }




    public function goodsImg(){
        $file =request()->file('file');
        $ret = $this->upload($file);
        return json($ret);
    }
    public function goodsImgDel(){
        $url = "../public".input('post.imgUrl');
        if (file_exists($url)){
            if (!unlink($url)){
                return json(['code'=>0,'msg'=>'删除失败']);
            }
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }

    public function cartesian(){
        $spec = input('post.spec');
        $key = input('post.key');
        $values = input('post.values');
        $keys = input('post.keys');
        $specs = [];
        $sets = [];
        $result = [];
        $spec_name = [];
        for ($i=0;$i<count($spec);$i++){
            $specs[$i]=[
                'spec'=>$spec[$i],
                'key'=>$key[$i]
            ];
            for ($j=0;$j<count($values);$j++){
                if ($key[$i] == $keys[$j]){
                    $specs[$i]['value'][$j] = $values[$j];
                }else{
                    $specs[$i]['value'] = [];
                }
            }
            $sets[$i] = $specs[$i]['value'];
            $spec_name[$i] = $specs[$i]['spec'];
        }

        // 循环遍历集合数据
        for($i=0,$count=count($sets); $i<$count-1; $i++){
            // 初始化
            if($i==0){
                $result = $sets[$i];
            }
            // 保存临时数据
            $tmp = array();
            // 结果与下一个集合计算笛卡尔积
            foreach($result as $res){
                foreach($sets[$i+1] as $set){
                    $tmp[] = $res.','.$set;
                }
            }
            // 将笛卡尔积写入结果

            $result = $tmp;
        }
        foreach ($result as $k=>$v){
            $result[$k] = explode(',',$v);
        }

        return json(['sku'=>$result,'name'=>$spec_name]);
    }

}